﻿package mortal.game.scene3D.player.entity
{
    import __AS3__.vec.*;
    import baseEngine.modifiers.*;
    import baseEngine.system.*;
    import com.gengine.utils.*;
    import com.gengine.utils.pools.*;
    import com.mui.utils.*;
    import flash.events.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.effectEditTool.temple.*;
    import frEngine.event.*;
    import frEngine.pools.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.rules.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.model.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.head.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.chat.data.*;
    import mortal.game.view.npc.*;
    import mortal.mvc.core.*;

    public class NPCPlayer extends Game2DPlayer implements IEntity
    {
        private var _npcInfo:NPCInfo;
        private var _selected:Boolean;
        private var _entityInfo:EntityInfo;
        private var _taskStatus:int = 0;
        private var _bodyPlayer:ActionPlayer;
        private var _hangPlayer:ActionPlayer;
        private var _effectPlayerArray:Vector.<EffectPlayer>;
        private var _statusPlayer:EffectPlayer;
        private var _isHasSpecialStandBy:Boolean = false;
        private var _nextSecialDate:Date;
        private var _headContainner:HeadContainer;
        private var _isNeerRole:Boolean = false;
        private var _nextTalkTime:int;
        private var _followTalkTime:int;
        private var _isAlpha:Boolean = false;
        private var _insertTime:int;
        private var _insertAngle:Number;
        protected var _direction:Number = 0;
        protected var _targetDirection:Number;
        private var _isFrameUpdateNamePosition:Boolean = false;
        private var _talkClearDate:Date;
        private var _talkText:TalkSprite;

        public function NPCPlayer()
        {
            this._effectPlayerArray = new Vector.<EffectPlayer>;
            this.initHeadContainner();
            this._entityInfo = ObjectPool.getObject(EntityInfo);
            overPriority = Game2DOverPriority.NPC;
            this.timerContorler = TimeControler.createTimerInstance();
            return;
        }// end function

        public function get type() : int
        {
            return EntityType.NPC;
        }// end function

        public function set isAlpha(param1:Boolean) : void
        {
            if (this._isAlpha != param1)
            {
                this._isAlpha = param1;
                if (this._isAlpha)
                {
                    this._bodyPlayer.alpha = 0.5;
                }
                else
                {
                    this._bodyPlayer.alpha = 1;
                }
            }
            return;
        }// end function

        public function updateInfo(param1:NPCInfo, param2:Boolean = true) : void
        {
            var _loc_3:int = 0;
            var _loc_4:Array = null;
            var _loc_5:Vector.<Array> = null;
            var _loc_6:int = 0;
            var _loc_7:Array = null;
            var _loc_8:Array = null;
            var _loc_9:EffectPlayer = null;
            this._npcInfo = param1;
            if (!this._entityInfo)
            {
                this._entityInfo = ObjectPool.getObject(EntityInfo);
            }
            this._entityInfo.entityInfo.name = this._npcInfo.tnpc.name;
            this._entityInfo.entityInfo.code = this._npcInfo.tnpc.code;
            this._entityInfo.entityInfo.level = int(this._npcInfo.tnpc.level);
            this.initPlayer();
            this.initHeadContainner();
            this._bodyPlayer.name = NPCUtil.getNPCPlayerName(this.npcInfo.snpc.npcId);
            if (this._npcInfo.snpc)
            {
                this.x2d = this._npcInfo.snpc.point.x;
                this.y2d = this._npcInfo.snpc.point.y;
                _loc_3 = GameMapUtil.getPixelPointValue(this._npcInfo.snpc.point.x, this._npcInfo.snpc.point.y);
                this.isAlpha = MapNodeType.isAlpha(_loc_3);
            }
            if (this._npcInfo.tnpc)
            {
                this.updateName();
                this._bodyPlayer.load(this._npcInfo.tnpc.mesh, this._npcInfo.tnpc.bone, this._npcInfo.tnpc.texture);
                this.updateDirection(this._npcInfo.tnpc.direction);
                if (this._npcInfo.tnpc.modelScale)
                {
                    this._bodyPlayer.scaleValue = this._npcInfo.tnpc.modelScale * 1 / 100;
                }
                this.updateHoverArea(this._npcInfo.tnpc.mouseArea);
                this._headContainner.updateSpecialTitle(this._npcInfo.tnpc.title);
                if (this._npcInfo.tnpc.hangModel)
                {
                    _loc_4 = this._npcInfo.tnpc.hangModel.split("#");
                    if (_loc_4.length >= 4)
                    {
                        if (!this._hangPlayer)
                        {
                            this._hangPlayer = FrObjectPool.getObject(ActionPlayer, this);
                            this._hangPlayer.changeAction(ActionName.Stand);
                            this._hangPlayer.selectEnabled = true;
                            this._hangPlayer.timerContorler = this.timerContorler;
                            this._hangPlayer.play();
                        }
                        this._hangPlayer.load(_loc_4[0], _loc_4[1], _loc_4[2]);
                        this._hangPlayer.hangBoneName = _loc_4[3];
                        this._bodyPlayer.hang(this._hangPlayer);
                    }
                }
                if (this._npcInfo.tnpc.selfEffect)
                {
                    _loc_5 = GameStrUtil.alzToArray(this._npcInfo.tnpc.selfEffect);
                    _loc_7 = _loc_5[0];
                    _loc_8 = _loc_5[1];
                    _loc_6 = 0;
                    while (_loc_6 < _loc_7.length)
                    {
                        
                        if (this._effectPlayerArray.length > _loc_6)
                        {
                            _loc_9 = this._effectPlayerArray[_loc_6];
                            this._bodyPlayer.unHang(_loc_9);
                            _loc_9.dispose();
                        }
                        _loc_9 = EffectPlayerPool.instance.getEffectPlayer(_loc_7[_loc_6], this._bodyPlayer, null, true, true);
                        this._effectPlayerArray[_loc_6] = _loc_9;
                        if (_loc_8.length > _loc_6)
                        {
                            _loc_9.hangBoneName = _loc_8[_loc_6];
                        }
                        if (_loc_9.temple is TempleRole)
                        {
                            (_loc_9.temple as TempleRole).setRoleParams(this._bodyPlayer, null, null);
                        }
                        else
                        {
                            this._bodyPlayer.hang(_loc_9);
                        }
                        _loc_9.play(PlayMode.ANIMATION_LOOP_MODE);
                        _loc_6++;
                    }
                }
            }
            this.updateStatus(this._npcInfo);
            this.updateIsNeerRole();
            return;
        }// end function

        protected function updateHoverArea(param1:String) : void
        {
            var _loc_2:Array = null;
            if (param1)
            {
                _loc_2 = param1.split("#");
                hoverLeft = int(_loc_2[0]);
                hoverTop = int(_loc_2[1]);
                hoverWidth = int(_loc_2[2]);
                hoverHeight = int(_loc_2[3]);
            }
            else
            {
                hoverLeft = 50;
                hoverTop = 120;
                hoverWidth = 100;
                hoverHeight = 140;
            }
            return;
        }// end function

        public function updateIsTalkToRole(param1:Boolean) : void
        {
            var _loc_2:Number = NaN;
            if (this._bodyPlayer != null)
            {
                if (param1)
                {
                    _loc_2 = MathUitl.getAngleByXY(this.x2d, this.y2d, RolePlayer.instance.x2d, RolePlayer.instance.y2d);
                    this.direction = _loc_2;
                }
                else
                {
                    this.direction = this._npcInfo.tnpc.direction;
                }
            }
            return;
        }// end function

        public function updateHeadContainer() : void
        {
            return;
        }// end function

        protected function initHeadContainner() : void
        {
            if (!this._headContainner)
            {
                this._headContainner = FrObjectPool.getObject(HeadContainer, this);
                this.addChild(this._headContainner);
            }
            return;
        }// end function

        public function specialStandBy() : void
        {
            if (this._bodyPlayer)
            {
                this.setActionName(ActionName.Standing);
                this.nextSpecialTime();
            }
            return;
        }// end function

        protected function nextSpecialTime() : void
        {
            var _loc_1:* = ClockManager.instance.nowDate.time + Math.random() * 10000 + 10000;
            this._nextSecialDate = new Date(_loc_1);
            ClockManager.instance.addDateCall(this._nextSecialDate, this.specialStandBy);
            return;
        }// end function

        protected function initPlayer() : void
        {
            if (!this._bodyPlayer)
            {
                this._bodyPlayer = FrObjectPool.getObject(ActionPlayer, this);
                this.setActionName(ActionName.Stand);
                this._bodyPlayer.selectEnabled = true;
                this._bodyPlayer.timerContorler = this.timerContorler;
                FrEventDispatcher.instance.proxyAddEventListener(this._bodyPlayer, Engine3dEventName.InitComplete, this.onInitBodyPlayer);
                this._bodyPlayer.play();
            }
            this.timerContorler.active();
            this.addChild(this._bodyPlayer);
            return;
        }// end function

        protected function onInitBodyPlayer(event:Event) : void
        {
            if (this._bodyPlayer)
            {
                this._isHasSpecialStandBy = this._bodyPlayer.hasAction(ActionName.Standing);
                if (this._isHasSpecialStandBy)
                {
                    this.nextSpecialTime();
                    this._bodyPlayer.addFrameComplete(ActionName.Standing, this.onFrameComplete);
                }
                this.updateNamePosition();
            }
            return;
        }// end function

        protected function onFrameComplete() : void
        {
            if (!this._bodyPlayer)
            {
                return;
            }
            this.setActionName(ActionName.Stand);
            return;
        }// end function

        public function setActionName(param1:String) : void
        {
            this._bodyPlayer.changeAction(param1);
            this.updateNamePosition();
            return;
        }// end function

        protected function updateNamePosition() : void
        {
            if (this._headContainner)
            {
                this._headContainner.y = this._bodyPlayer.getBoneGlobleY(this._headContainner.hangBoneName) - 10;
            }
            if (this._statusPlayer)
            {
                this._statusPlayer.y = this._bodyPlayer.getBoneGlobleY(this._headContainner.hangBoneName) + 30;
            }
            return;
        }// end function

        public function addToStage(param1:SLayer3D) : Boolean
        {
            if (!param1.contains(this))
            {
                param1.addChild(this);
                this._headContainner.show();
                return true;
            }
            return false;
        }// end function

        public function removeFromStage(param1:SLayer3D, param2:Boolean = false) : Boolean
        {
            if (this.parent == param1)
            {
                param1.removeChild(this);
                this._headContainner.hide();
                return true;
            }
            return false;
        }// end function

        public function updateStatus(param1:NPCInfo) : void
        {
            if (this._taskStatus == param1.taskStatus)
            {
                return;
            }
            this._taskStatus = this._npcInfo.taskStatus;
            if (this._statusPlayer)
            {
                this._statusPlayer.dispose();
                this._statusPlayer = null;
            }
            if (this._npcInfo.taskStatus == NpcTaskStatus.HasCompleteTask)
            {
                this._statusPlayer = EffectPlayerPool.instance.getEffectPlayer(GlobalStaticResUrl.NPCHasCompleteTaskEffect, this._bodyPlayer, null, true, true);
                this.addChild(this._statusPlayer);
                this._statusPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
            }
            else if (this._npcInfo.taskStatus == NpcTaskStatus.HasCanGetTask)
            {
                this._statusPlayer = EffectPlayerPool.instance.getEffectPlayer(GlobalStaticResUrl.NPCHasHasCanGetTask, this._bodyPlayer, null, true, true);
                this.addChild(this._statusPlayer);
                this._statusPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
            }
            this.updateNamePosition();
            return;
        }// end function

        public function updateName() : void
        {
            this._headContainner.updateName(this._npcInfo.tnpc.name);
            return;
        }// end function

        public function set selected(param1:Boolean) : void
        {
            if (this._selected == param1)
            {
                return;
            }
            var _loc_2:* = SceneGlobalPlayer.selectPlayerMask;
            this._selected = param1;
            if (param1)
            {
                if (this.contains(_loc_2) == false)
                {
                    this.addChild(_loc_2);
                    _loc_2.play(PlayMode.ANIMATION_LOOP_MODE);
                }
            }
            else if (this.contains(_loc_2))
            {
                _loc_2.stop();
                this.removeChild(_loc_2);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.Player_Selected, this));
            return;
        }// end function

        public function get toRoleDistance() : Number
        {
            var _loc_1:* = this.x2d - RolePlayer.instance.x2d;
            var _loc_2:* = this.y2d - RolePlayer.instance.y2d;
            return _loc_1 * _loc_1 + _loc_2 * _loc_2;
        }// end function

        override public function hoverTest(param1:Number, param2:Number, param3:Number, param4:Number) : Boolean
        {
            var _loc_5:* = hoverLeft;
            var _loc_6:* = hoverTop;
            var _loc_7:* = hoverWidth;
            var _loc_8:* = hoverHeight;
            if (this._bodyPlayer)
            {
                _loc_7 = _loc_7 * this._bodyPlayer.scaleX;
                _loc_8 = _loc_8 * this._bodyPlayer.scaleY;
                _loc_5 = _loc_5 * this._bodyPlayer.scaleX;
                _loc_6 = _loc_6 * this._bodyPlayer.scaleY;
            }
            _displayRec.x = this.x2d - param1 - _loc_5;
            _displayRec.y = this.y2d - param2 - _loc_6;
            _displayRec.width = _loc_7;
            _displayRec.height = _loc_8;
            return _displayRec.contains(param3, param4);
        }// end function

        override public function onMouseOut() : void
        {
            Global3D.scene.removeSelectObject(this._bodyPlayer, false);
            return;
        }// end function

        override public function onMouseOver() : void
        {
            Global3D.scene.addSelectObject(this._bodyPlayer, false, 14596231);
            return;
        }// end function

        public function updateIsNeerRole() : void
        {
            var _loc_1:* = this.toRoleDistance < 40000;
            if (_loc_1 && !this._isNeerRole)
            {
                this.neerRole();
            }
            this._isNeerRole = _loc_1;
            return;
        }// end function

        protected function neerRole() : void
        {
            this.topTalk(this._npcInfo.npcNeerTalkId);
            return;
        }// end function

        public function closeSpecialWindow() : void
        {
            this.topTalk(this._npcInfo.npcCloseTalkId);
            return;
        }// end function

        protected function topTalk(param1:int) : void
        {
            var _loc_2:TalkInfo = null;
            var _loc_3:String = null;
            if (param1 && (this._nextTalkTime == 0 || this._nextTalkTime < getTimer()))
            {
                this.clearFollowTalk();
                _loc_2 = TalkConfig.instance.getInfoById(param1);
                _loc_3 = _loc_2.randomTalk();
                if (_loc_3)
                {
                    LayerManager.entityTalkLayer.addTalk(this, _loc_3);
                    this._nextTalkTime = getTimer() + _loc_2.talkNextTime * 1000;
                    if (_loc_2.followTalk)
                    {
                        this.followTalk(_loc_2.followTalk);
                    }
                    if (_loc_2.followAction)
                    {
                        this.setActionName(_loc_2.followAction);
                    }
                }
            }
            return;
        }// end function

        protected function followTalk(param1:int) : void
        {
            this._followTalkTime = setTimeout(this.topTalk, 5000, param1);
            return;
        }// end function

        protected function clearFollowTalk() : void
        {
            if (this._followTalkTime)
            {
                clearTimeout(this._followTalkTime);
                this._followTalkTime = 0;
            }
            return;
        }// end function

        override public function update(param1:int = 0) : void
        {
            if (this._insertTime > 0)
            {
                if (this._insertTime > 1)
                {
                    this.updateDirection(this._direction + this._insertAngle);
                    var _loc_2:String = this;
                    var _loc_3:* = this._insertTime - 1;
                    _loc_2._insertTime = _loc_3;
                }
                else
                {
                    this.updateDirection(this._targetDirection);
                    this._insertTime = 0;
                }
            }
            super.update(param1);
            return;
        }// end function

        public function updateDirection(param1:Number) : void
        {
            if (param1 > 180)
            {
                param1 = param1 - 360;
            }
            else if (param1 < -180)
            {
                param1 = param1 + 360;
            }
            this._direction = param1;
            this.updateBodyDirection();
            return;
        }// end function

        protected function updateBodyDirection() : void
        {
            if (this._bodyPlayer)
            {
                this._bodyPlayer.direction = this._direction;
            }
            return;
        }// end function

        override public function set direction(param1:Number) : void
        {
            var _loc_2:Number = NaN;
            this._targetDirection = param1;
            _loc_2 = this._targetDirection - this._direction;
            if (_loc_2 > 180)
            {
                _loc_2 = _loc_2 - 360;
            }
            else if (_loc_2 < -180)
            {
                _loc_2 = _loc_2 + 360;
            }
            this._insertTime = Math.abs(Math.floor(_loc_2 / 12));
            this._insertTime = this._insertTime < 6 ? (6) : (this._insertTime);
            this._insertTime = this._insertTime > 12 ? (12) : (this._insertTime);
            this._insertAngle = _loc_2 / this._insertTime;
            if (this._insertTime == 0)
            {
                this.updateDirection(this._targetDirection);
            }
            return;
        }// end function

        override public function get direction() : Number
        {
            return this._direction;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            if (ThingUtil.selectEntity == this)
            {
                ThingUtil.selectEntity = null;
            }
            this._targetDirection = 0;
            this._insertTime = 0;
            this._insertAngle = 0;
            this._direction = 0;
            this._taskStatus = 0;
            this._npcInfo = null;
            this._entityInfo.dispose();
            this._entityInfo = null;
            this._effectPlayerArray.length = 0;
            if (this._hangPlayer)
            {
                this._bodyPlayer.unHang(this._hangPlayer);
                this._hangPlayer.dispose();
                this._hangPlayer = null;
            }
            if (this._statusPlayer)
            {
                this._statusPlayer.dispose();
                this._statusPlayer = null;
            }
            if (this._bodyPlayer)
            {
                this._bodyPlayer.dispose();
                this._bodyPlayer = null;
            }
            if (this._headContainner)
            {
                this._headContainner.dispose();
                this._headContainner = null;
            }
            this.timerContorler.unActive();
            this._isHasSpecialStandBy = false;
            if (this._nextSecialDate)
            {
                ClockManager.instance.removeDateCall(this._nextSecialDate);
                this._nextSecialDate = null;
            }
            LayerManager.entityTalkLayer.removeTalk(this);
            if (!param1)
            {
                TimeControler.disposeTimer(this.timerContorler);
            }
            this._isNeerRole = false;
            this._nextTalkTime = 0;
            this.clearFollowTalk();
            super.dispose(param1);
            return;
        }// end function

        override protected function disposeObject() : void
        {
            FrObjectPool.disposeObject(this, NPCPlayer);
            return;
        }// end function

        public function get npcInfo() : NPCInfo
        {
            return this._npcInfo;
        }// end function

        public function get entityID() : String
        {
            return "";
        }// end function

        public function get selected() : Boolean
        {
            return this._selected;
        }// end function

        public function get entityInfo() : EntityInfo
        {
            if (this._entityInfo == null)
            {
                this._entityInfo = new EntityInfo();
            }
            return this._entityInfo;
        }// end function

        public function setAction(param1:String, param2:String, param3:Boolean = false) : void
        {
            return;
        }// end function

        public function get actionName() : String
        {
            return ActionName.Stand;
        }// end function

        public function walking(param1:Array) : void
        {
            return;
        }// end function

        public function attack() : void
        {
            return;
        }// end function

        public function set actionEffect(param1:String) : void
        {
            return;
        }// end function

        public function hurt(param1:int) : void
        {
            return;
        }// end function

        public function get isDead() : Boolean
        {
            return false;
        }// end function

        public function set isDead(param1:Boolean) : void
        {
            return;
        }// end function

        public function showName(param1:Boolean) : void
        {
            return;
        }// end function

        public function showLife(param1:Boolean) : void
        {
            return;
        }// end function

        public function showTitleMain() : void
        {
            return;
        }// end function

        public function get entityStatus() : EntityStatus
        {
            return null;
        }// end function

        public function set mouseOver(param1:Boolean) : void
        {
            return;
        }// end function

        public function talk(param1:String, param2:Number = 5000) : void
        {
            if (param1 != null && param1 != "")
            {
                if (this._talkText == null)
                {
                    this._talkText = UICompomentPool.getUICompoment(TalkSprite);
                }
                if (this._talkClearDate)
                {
                    ClockManager.instance.removeDateCall(this._talkClearDate);
                }
                this._talkClearDate = ClockManager.instance.getDelayDate(param2);
                ClockManager.instance.addDateCall(this._talkClearDate, this.timeoutClearTalk);
                LayerManager.entityTalkLayer.addPopUp(this._talkText);
                this._talkText.faceAuthortiy = FaceAuthority.getFullAuthority();
                this._talkText.text = param1;
                this.updateTalkXY();
            }
            else
            {
                this.clearTalk();
            }
            return;
        }// end function

        protected function timeoutClearTalk() : void
        {
            LayerManager.entityTalkLayer.removeTalk(this);
            return;
        }// end function

        public function clearTalk() : void
        {
            if (this._talkText)
            {
                LayerManager.entityTalkLayer.removePopup(this._talkText);
                this._talkText.dispose();
                this._talkText = null;
                if (this._talkClearDate)
                {
                    ClockManager.instance.removeDateCall(this._talkClearDate);
                    this._talkClearDate = null;
                }
            }
            return;
        }// end function

        public function updateTalkXY() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            if (this._talkText)
            {
                _loc_1 = this.x2d - SceneRange.display.x;
                _loc_2 = this.y2d - SceneRange.display.y - 160;
                this._talkText.x = _loc_1;
                this._talkText.y = _loc_2;
            }
            return;
        }// end function

    }
}
